/// <reference path="Application_koViewModel.g.js" />
/// <reference path="../Scripts/jquery-1.7.1.min.js" />
/// <reference path="../Scripts/knockout-2.1.0.js" />

function ApplicationViewModel() {


    var self = this;
    // put a property loop here


    self.ApplicationID = ko.observable();
    self.ApplicationName = ko.observable();
    self.TS = ko.observable();
    self.CreatedBy = ko.observable();
    self.CreatedDt = ko.observable();
    self.ModifiedBy = ko.observable();
    self.ModifiedDt = ko.observable();
    self.Action = ko.observable();
    self.Caller = ko.observable();
    self.Role = ko.observable();
    self.PageSize = ko.observable();
    self.PageNumber = ko.observable();
    self.SortColumn = ko.observable();
    self.SortType = ko.observable();
    self.TotalRows = ko.observable();



    self.ApplicationList = ko.observableArray([]);
    self.setApplication = function (
			_ApplicationID, _ApplicationName, _TS, _CreatedBy, _CreatedDt, _ModifiedBy, _ModifiedDt, _Action, _Caller, _Role, _PageNumber, _PageSize, _SortColumn, _SortType, _TotalRows

	) {

        self.ApplicationID = _ApplicationID;
        self.ApplicationName = _ApplicationName;
        self.TS = _TS;
        self.CreatedBy = _CreatedBy;
        self.CreatedDt = _CreatedDt;
        self.ModifiedBy = _ModifiedBy;
        self.ModifiedDt = _ModifiedDt;
        self.Action = _Action;
        self.Caller = _Caller;
        self.Role = _Role;
        self.PageSize = _PageSize
        self.PageNumber = _PageNumber;
        self.SortColumn = _SortColumn;
        self.SortType = _SortType;
        self.TotalRows = _TotalRows;

    }; //  End of set procedure 

    //Add Application
    self.AddApplication = function () {

        alert("We are in the AddApplication ko function");

        self.Action = "Add";
        self.Caller = "trooks";
        self.Role = "user";
        self.CreatedBy = "trooks";
        self.CreatedDt = "06/22/2013";
        self.ModifiedBy = "trooks";
        self.ModifiedDt = "06/22/2013";

        jQuery.ajax({
            url: "/api/ApplicationAPI",
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, CreatedBy: self.CreatedBy, CreatedDt: self.CreatedDt, ModifiedBy: self.ModifiedBy, ModifiedDt: self.ModifiedDt, ApplicationName: self.ApplicationName },
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                alert(result);
            }
        });



    }; //End of add procedure

    //Load Application
    self.LoadApplication = function (appVal) {
        self = appVal;
        alert("Load Application " + self.ApplicationID);

    };
    //Delete Application
    self.DeleteApplicationAction = function () {
        //alert("Delete Action " + self.ApplicationID);
        self.Action = "Delete";
        self.Caller = "trooks";
        self.Role = "user";
        self.CreatedBy = "trooks";
        self.CreatedDt = "06/22/2013";
        self.ModifiedBy = "trooks";
        self.ModifiedDt = "06/22/2013";


        jQuery.ajax({
            url: "/api/ApplicationAPI",
            data: { ApplicationName: "", Action: self.Action, Caller: self.Caller, CreatedBy: self.CreatedBy, CreatedDt: self.CreatedDt, Role: self.Role, ModifiedBy: self.ModifiedBy, ModifiedDt: self.ModifiedDt },
            type: 'POST'


        }).done(function (data) {

            $.each(data, function (key, val) {
                alert("We have loaded the values and are in the each module. " + JSON.stringify(val));

            })
        })
    };
    //Edit Application
    self.UpdateApplication = function () {

        alert("We are in the Edit Application " + self.ApplicationName);

        self.Action = "Load";
        self.Caller = "trooks";
        self.Role = "user";
        self.CreatedBy = "trooks";
        self.CreatedDt = "06/22/2013";
        self.ModifiedBy = "trooks";
        self.ModifiedDt = "06/22/2013";

        //var myData = ko.toJSON(self);
        jQuery.ajax({
            url: "/api/ApplicationAPI",
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, CreatedBy: self.CreatedBy, CreatedDt: self.CreatedDt, ModifiedBy: self.ModifiedBy, ModifiedDt: self.ModifiedDt },
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                alert(result);
            }
        });



    }; //End of add procedure
    //Sort Application
    self.sortRecords = ko.observableArray();

    ko.bindingHandlers.sort = {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var asc = false;
            element.style.cursor = 'pointer';

            element.onclick = function () {
                var value = valueAccessor();
                var prop = value.prop;
                var data = value.arr;

                asc = !asc;
                if (asc) {
                    self.ApplicationList.sort(function (left, right) {
                        return left[prop] == right[prop] ? 0 : left[prop] < right[prop] ? -1 : 1;
                    });
                } else {
                    self.ApplicationList.sort(function (left, right) {
                        return left[prop] == right[prop] ? 0 : left[prop] > right[prop] ? -1 : 1;
                    });
                }
            }
        }
    };


    //filter






    self.GetApplicationByParameter = function () {
        alert("We are inside of Get application by parameter funciton");
        self.Action = "GetAll";
        self.Caller = "trooks";
        self.Role = "user";
        self.PageNumber = 1;
        self.ApplicationName = "MyApp";
        self.PageSize = 10;
        self.SortColumn = "ApplicationID";
        self.SortType = "asc";
        jQuery.ajax({
            url: "/api/ApplicationAPI",
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, PageNumber: self.PageNumber, PageSize: self.PageSize, SortColumn: self.SortColumn, SortType: self.SortType },
            type: 'GET'


        }).done(function (data) {
            $.each(data, function (key, val) {

                self.ApplicationList.push(
                    val
                )
            })
        })

    };


}; //End of view model





